﻿using System;
using System.Web;
using ARS.Control.Custom.DataPaging.Attributes;
using ARS.Control.Custom.DataPaging.Widget;
using ARS.Control.Factory;
using ARS.PublicTool.DataHandle;

namespace ARS.Control.Custom.DataPaging.Request
{
    public class Action11 : IHttpHandler
    {
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.Form["action"];
            if (action != null)
            {
                //int dbId = 9;
                //string userId = context.Request.Form["userId"];

                Interface.IUser user=UserFactory.CreateUser();

                string userId=user.GetUserInfo().Id.ToString();

                //获取datapaging model
                DataBase.Provider.IDataPagingProvider dataProvider = ARS.DataBase.Provider.DataPagingProvider.GetInstance();
                DataPagingDb dbModel = dataProvider.GetDataPagingDbModel("Status_UserId", userId);

                if (action == "getlist")
                {
                    string pageIndex = context.Request.Form["PageIndex"]??"1";
                    string orderIndex = context.Request.Form["OrderIndex"] ?? "-1";
                    if (orderIndex != "-1")
                    {
                        dbModel.DbSelect.OrderField = dbModel.DbOperat.OrderListItem[Int32.Parse(orderIndex)];
                    }
                    dbModel.DbSelect.PageIndex = Int32.Parse(pageIndex);
                    context.Response.Write(Factory.Dp_HtmlHelperFactory.CreateHtmlHelper().GetTableHtml(dbModel));
                    return;
                }
                else if (action == "del")
                {
                    string id = context.Request.Form["id"];
                    if (id != null)
                    {
                        id = Encrypt.DecryptString(id);

                        ARS.Control.Interface.IPermDataPaging perm =
                            ARS.Control.Factory.PermDataPaging.CreatePermDataPaging();
                        if (perm.CheckDelPerm(dbModel))
                        {

                        }
                        else
                        {
                            context.Response.Write(JsonSerializer.GetJson(0, "没有权限删除"));
                            return;
                        }

                        ARS.DataBase.Provider.IDbProvider dbProvider = ARS.DataBase.Provider.DbProvider.GetInstance();
                        string deleteSqlStr = null;
                        if (string.IsNullOrEmpty(dbModel.DbOperat.DeleteSql))
                        {
                            deleteSqlStr = string.Format("delete from {0} where {1}={2}",dbModel.DbSelect.TableName,dbModel.DbSelect.KeyField,id);
                        }
                        else
                        {
                            deleteSqlStr = string.Format(dbModel.DbOperat.DeleteSql,id);
                        }
                        if (dbProvider.ExecuteSql(deleteSqlStr) > 0)
                        {
                            context.Response.Write(JsonSerializer.GetJson(1, "su"));
                        }
                        else
                        {
                            context.Response.Write(JsonSerializer.GetJson(0, "删除失败"));
                        }
                        
                        return;
                    }
                }
                else if (action == "ExecuteOperat")
                {
                    string index = context.Request.Form["index"];
                    if (index == null)
                    {

                    }
                    else
                    {
                        string[] list = dbModel.DbOperat.OperatSqlListItem;
                        if (list != null)
                        {
                           
                            string sqlStr = list[Int32.Parse(index)];
                            if (sqlStr != null)
                            {
                                string id = context.Request.Form["id"];
                                id = Encrypt.DecryptString(id);
                                sqlStr = sqlStr.Replace("{0}",id);
                                if (sqlStr.Contains("{{Text-"))
                                {
                                    //Regex regex_field = new Regex("");
                                    //MatchCollection matchs = regex_field.Matches(sqlStr);
                                    sqlStr = Factory.Dp_HtmlHelperFactory.CreateHtmlHelper().OperatPostParams(sqlStr);
                                    //HtmlHelper.OperatPostParams(sqlStr);
                                }
                                ARS.DataBase.Provider.IDbProvider dbProvider = ARS.DataBase.Provider.DbProvider.GetInstance();
                                int count = dbProvider.ExecuteSql(sqlStr);
                                context.Response.Write(JsonSerializer.GetJson(count, index));
                            }
                        }
                    }
                }
            }
            //在此写入您的处理程序实现。
            //context.Response.Write("123");
        }

        #endregion
    }
}
